package com.eshop.eshop2021_v1.controller.before;

import com.eshop.eshop2021_v1.common.Email;
import com.eshop.eshop2021_v1.common.JsonResult;
import com.eshop.eshop2021_v1.pojo.User;
import com.eshop.eshop2021_v1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author: 若若叶
 * @date: 2021/10/5 16:08 FileName: UserController Description:
 */
@Controller
@RequestMapping("/user")
public class UserController {
  @Autowired private UserService userService;
  /**
   * 跳转到注册页面
   *
   * @return 是的
   */
  @GetMapping("/register")
  public String toRegPage() {
    return "user/register";
  }

  // 检查邮箱
  @RequestMapping("/checkedEmail")
  @ResponseBody
  public JsonResult checkedEmail(@RequestParam("email") String email) {
    JsonResult result = userService.checkedEmail(email);
    return result;
  }

  // 发送email
  @RequestMapping("/sendEmail")
  @ResponseBody
  public JsonResult sendEmail(String email, HttpSession session) {
    String code = Email.sendEmail(email);
    session.setAttribute("code", code);
    return new JsonResult(200, "发送成功", null);
  }

  // 执行注册
  //        var param = {"email": email, "password": password,
  //        "comfirmPwd": comfirmPwd, "code":
  // code};
  @PostMapping("/doReg")
  @ResponseBody
  public JsonResult doReg(
      String email, String password, String comfirmPwd, String code, HttpSession session) {
    JsonResult result = userService.doReg(email, password, comfirmPwd, code, session);
    return result;
  }

  @GetMapping("/toLogin")
  public String toLoginPaga() {
    return "user/login";
  }

  // 去修改密码页面
  @GetMapping("/toUserInfo")
  public String toUserInfoPage() {
    return "user/userInfo";
  }

  // 去修改个人信息页面
  @GetMapping("/toUserInfoUpdate")
  public String toUserInfoUpdatePage() {
    return "user/userInfoUpdate";
  }
  // 登录
  @PostMapping("/doLogin")
  public String doLogin(User user, String code, HttpSession session, Model model) {
    String view = userService.doLogin(user, code, session, model);
    return view;
  }
  // 通过邮箱修改个人信息
  @RequestMapping("/updateByEmail")
  @ResponseBody
  public JsonResult updateByEmail(User user) {
    return userService.updateByEmail(user);
  }
  // 修改密码
  @PostMapping("/doUpdatePwd")
  @ResponseBody
  public JsonResult doUpdatePwd(String email, String password) {
    return userService.doUpdatePwd(email, password);
  }

  @RequestMapping("/showInfoFrom")
  public String showInfoFrom() {
    return "user/userInfoFrom";
  }
}
